package util;

import com.google.gson.Gson;
import message.Message;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;

public interface Serializer {
  <T> T deserialize(Class<T> clazz,byte[] bytes);
  <T> byte[] serialize(T object);

  enum Algorithm implements Serializer{
    Java{
      @Override
      public <T> T deserialize(Class<T> clazz, byte[] bytes) {
        try {
          ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
          return  (T) ois.readObject();
        } catch (IOException e) {
          throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
          throw new RuntimeException(e);
        }
      }

      @Override
      public <T> byte[] serialize(T object) {
        try {
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          ObjectOutputStream oos = new ObjectOutputStream(bos);
          oos.writeObject(object);
          return bos.toByteArray();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    },
    Json{
      @Override
      public <T> T deserialize(Class<T> clazz, byte[] bytes) {
        String json = new String(bytes, StandardCharsets.UTF_8);
        return new Gson().fromJson(json,clazz);
      }

      @Override
      public <T> byte[] serialize(T object) {
        String json = new Gson().toJson(object);
        return json.getBytes(StandardCharsets.UTF_8);
      }
    }
  }
}
